window.onload = function(){
	checkAdminLogined();
}
function checkAdminLogined(){
	//判断是否已经登录管理员，如果未登录，返回用户主页
	var adminMsg = sessionStorage.getItem('adminMsg');
	if(adminMsg==undefined){
		alert("你尚未以管理员身份登录，请先以管理员身份登录");
		window.location.href="review.jsp";
	}
}
function logutSafety(){
	//注销管理员
	sessionStorage.removeItem('adminMsg');
	alert("已退出");
	window.location.href="review.jsp";
}
//添加电影
function addMovie(work){
	var makemessage = document.getElementById("makemessage");
	makemessage.innerHTML="<div class=\"addMessage\" id=\"addMovie\"><form enctype=\"multipart/form-data\" id=\"formid\" action=\"http://127.0.0.1:8080/webXiangmu/add_reviceMovieServlet\" method=\"post\" autocomplete=\"off\">"+
	"<h2>添加电影</h2>"+
	"<p><span>电影名称</span><input class=\"inputtext\" type=\"text\" name=\"mname\" id=\"mname\" ></p>"+
	"<p><span>电影类型</span><input class=\"inputtext\" type=\"text\" name=\"type\" id=\"type\" placeholder=\"多个类型请用','隔开\" multiple=\"multiple\">"+
	"</p>"+
	"<p><span>导&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;演</span><input class=\"inputtext\" type=\"text\" name=\"daoyan\" id=\"daoyan\" placeholder=\"输入多人请用','隔开\" multiple=\"multiple\"></p>"+
	"<p><span>主&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;演</span><input class=\"inputtext\" type=\"text\" name=\"zhuyan\" id=\"zhuyan\" placeholder=\"输入多人请用','隔开\" multiple=\"multiple\"></p>"+
	"<p><span>上映时间</span><input class=\"inputtext\" type=\"date\" id=\"showtime\" name=\"showtime\" ></p>"+
	"<p><span>详细介绍</span></p>"+
	"<p><textarea id=\"mcontent\" name=\"mcontent\" cols=\"50\" rows=\"5\"></textarea></p>"+
	"<p><span>海报</span><input type=\"file\" id=\"poster\" name=\"poster\"></p>"+
	"<p><span>预告片</span><input type=\"file\" id=\"trailer\" name=\"trailer\"></p>"+
	"<p id=\"btn\">  <input class=\"submitbtn\" type=\"button\" onclick=\"checkMovie()\" value=\"发布\"/></p>"+
	"<input name=\"work\" id=\"work\" type=\"hidden\"value=\""+work+"\">" +
	"<input name=\"mid\" id=\"mid\" type=\"hidden\"value=\"\">"+
	"</form></div>";
}
function checkMovie(){
	var mname = document.getElementById("mname");
	var type = document.getElementById("type");
	var daoyan = document.getElementById("daoyan");
	var zhuyan = document.getElementById("zhuyan");
	var showtime = document.getElementById("showtime");
	var mcontent = document.getElementById("mcontent");
	var poster = document.getElementById("poster");
	var trailer = document.getElementById("trailer");
	if(mname.value==""){
		alert("请输入电影名称");
		return false;
	}
	if(type.value==""){
		alert("请输入电影类型");
		return false;
	}
	if(daoyan.value==""){
		alert("请输入导演");
		return false;
	}
	if(zhuyan.value==""){
		alert("请输入主演");
		return false;
	}
	if(showtime.value==""){
		alert("请输入上映时间");
		return false;
	}
	if(mcontent.value==""){
		alert("请输入电影介绍");
		return false;
	}
	if(poster.value!=""&&!/\.(jpg|png)$/.test(poster.value)){
		alert("请选择jpg或png图片");
		return false;
	}
	if(trailer.value!=""&&!/\.(mp4)$/.test(trailer.value)){
		alert("请选择MP4文件");
		return false;
	}
	document.getElementById("formid").submit();
}
//电影修改页面
function reviseMovie(){
	var json = {
			instruct:"reviseMovie"
	}
	goadminAjax(json);
}
//添加影评
function addReview(work){
	var makemessage = document.getElementById("makemessage");
	makemessage.innerHTML="<div class=\"addMessage\" id=\"addReview\">"+
	"<form id=\"formid\" action=\"http://127.0.0.1:8080/webXiangmu/AdminServlet\" method=\"post\" autocomplete=\"off\">"+
		"<h2>添加影评</h2>"+
			"<p><span>影评标题</span><input class=\"inputtext\" type=\"text\" name=\"reviewTitle\" id=\"reviewTitle\"></p>"+		
			"<p><span>电影名称</span>"+
				"<select class=\"inputtext\" name=\"movieSel\" id=\"movieSel\">"+
//					"<option value=\"1\">复仇者联盟</option>"+
				"</select></p>"+	
			"<p><span>影评内容</span></p>"+
			"<p><textarea name=\"rcontent\" id=\"rcontent\" cols=\"50\" rows=\"5\"></textarea></p>"+
			"<p><span>作&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;者</span><input class=\"inputtext\" type=\"text\" id=\"author\" name=\"author\"></p>"+
			"<p id=\"btn\">  <input class=\"submitbtn\" type=\"button\" onclick=\"checkReview()\" value=\"发布\"/></p>"+
		"<input type=\"hidden\" id=\"work\" name=\"work\" value="+work+" />"+
		"<input type=\"\hidden\" id=\"rid\" name=\"rid\"value=\"\">"
	"</form></div>";
	var json = {
			instruct:"addReview"
	}
	goadminAjax(json);
}
//影评页面提交判断
function checkReview(){
	var reviewTitle = document.getElementById("reviewTitle");
	var movieSel = document.getElementById("movieSel");
	var rcontent = document.getElementById("rcontent");
	var author = document.getElementById("author");
	//alert("reviewTitle="+reviewTitle.value+" movieSel="+movieSel.value+" rcontent="+rcontent.value+" author="+author.value);
	if(reviewTitle.value == ""){
		alert("请输入影评标题");
		return false;
	}
	if(rcontent.value==""){
		alert("请输入影评内容");
		return false;
	}
	if(author.value==""){
		alert("请输入影评作者");
		return false;
	}
	document.getElementById("formid").submit();
}
//影评修改页面
function reviseReview(){
	var json = {
			instruct:"reviseReview"
	}
	goadminAjax(json);
}
//回复管理
function reviseComment(){
	var json = {
			instruct:"reviseComment"
	}
	goadminAjax(json);
}
//公告添加
function addNotice(work){
	var makemessage = document.getElementById("makemessage");
	makemessage.innerHTML="<div class=\"addMessage\" id=\"addNotice\">"+
	"<form id=\"formid\" action=\"http://127.0.0.1:8080/webXiangmu/AdminServlet\" method=\"post\" autocomplete=\"off\">"+
	"<h2>添加公告</h2>"+
		"<p><span>公告标题</span><input class=\"inputtext\" type=\"text\" name=\"noticename\" id=\"noticename\"></p>"+		
		"<p><span>公告内容</span></p>"+
		"<p><textarea name=\"noticecon\" id=\"noticecon\" cols=\"50\" rows=\"20\"></textarea></p>"+
		"<p id=\"btn\">  <input class=\"submitbtn\" type=\"button\" onclick=\"checkNotice()\" value=\"发布\"/></p>"+
	"<input name=\"work\" id=\"work\" type=\"hidden\" value=\""+work+"\" />"+
	"<input name=\"nid\" id=\"nid\" type=\"hidden\" value=\"\" />"+
	"</form></div>"
}
//公告提交判断
function checkNotice(){
	var noticetitle = document.getElementById("noticename");
	var noticecon = document.getElementById("noticecon");
	if(noticetitle.value==""){
		alert("标题不可为空");
		return false;
	}
	if(noticecon.value==""){
		alert("内容不可为空");
		return false;
	}
	document.getElementById("formid").submit();
}
//公告修改页面
function reviseNotice(){
	var json = {
			instruct:"reviseNotice"
	}
	goadminAjax(json);
}
//用户管理
function reviseUser(){
	var json = {
			instruct:"reviseUser"
	}
	goadminAjax(json);
}
//管理员设置
function reviseAdmin(){
	//alert(sessionStorage.getItem('adminMsg'));
	var adminMsg = JSON.parse(sessionStorage.getItem('adminMsg'));
	var aid = adminMsg.aId;
	var uname = adminMsg.uname;
	var addtime = adminMsg.addtime;
	var lasttime = adminMsg.lasttime;
	//alert("aid="+aid+" uname="+uname+");
	var makemessage = document.getElementById("makemessage");
	makemessage.innerHTML=
	"<table id=\"table\" class=\"table\">"+
	"<tr class=\"thead\">"+
	"<td class=\"id\">管理员ID</td>"+
	"<td>账号</td>"+
	"<td class=\"time\">最后一次登录时间</td>"+
	"<td class=\"time\">注册时间</td>"+
	"</tr>"+
	"<tr class=\"tcom\">"+
	"<td class=\"id\"><p>"+aid+"</p></td>"+
	"<td><p>"+uname+"</p></td>"+
	"<td class=\"time\"><p>"+lasttime+"</p></td>"+
	"<td class=\"time\"><p>"+addtime+"</p></td>"+
	"</tr>"+
	"</table>"+
	"<div class=\"addMessage\" id=\"addNotice\">"+
	"<form id=\"formid\" action=\"http://127.0.0.1:8080/webXiangmu/AdminServlet\" method=\"post\" autocomplete=\"off\">"+
	"<h2>管理员信息</h2>"+
		"<p><span>更改密码</span><input class=\"inputtext\" type=\"password\" name=\"oldPassword\" id=\"oldPassword\"></p>"+		
		"<p><span>新&nbsp;&nbsp;密&nbsp;&nbsp;码</span><input class=\"inputtext\" type=\"password\" name=\"newPassword1\" id=\"newPassword1\"></p>"+
		"<p><span>再次输入</span><input class=\"inputtext\" type=\"password\" name=\"newPassword2\" id=\"newPassword2\"></p>"+
		"<p id=\"btn\">  <input class=\"submitbtn\" type=\"button\" onclick=\"checkreviAdmin()\" value=\"提交\"/></p>" +
	"<input type=\"hidden\" name=\"work\" value=\"reviseAdmin\" />"+
	"<input type=\"hidden\" name=\"aid\" value=\""+aid+"\">"+
	"</form></div>";
}
//管理员修改密码提交
function checkreviAdmin(){
	var oldPassword = document.getElementById("oldPassword");
	var newPassword1 = document.getElementById("newPassword1");
	var newPassword2 = document.getElementById("newPassword2");
	//alert("oldPassword="+oldPassword.value+" newPassword1="+newPassword1.value+" newPassword2="+newPassword2.value);
	var adminMsg = sessionStorage.getItem('adminMsg');
	var adminMsgJson = JSON.parse(adminMsg);
	var realoldPassword = adminMsgJson.password;
	//var realoldPassword=JSON.parse(sessionStorage.getItem('adminMsg')).password;
	//alert("realoldPassword="+realoldPassword);
	if(oldPassword.value==""||newPassword1.value==""||newPassword2.value==""){
		alert("请输入完整的信息!");
		return false;
	}else{
		if(realoldPassword!=oldPassword.value){
			alert("密码错误！");
			return false;
		}else{
			if(newPassword1.value!=newPassword2.value){
				alert("两次密码不相同！");
				return false;
			}
		}
	}
	adminMsgJson.password = newPassword1.value;
	adminMsg = JSON.stringify(adminMsgJson);
	sessionStorage.setItem('adminMsg', adminMsg);
	//alert("password更改："+JSON.parse(sessionStorage.getItem('adminMsg')).password);
	document.getElementById("formid").submit();
}			